/**
 * @Title: integrityArtificialScore_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2015
 * Company:中国平安
 *
 * @Project mhis-fwa-131
 * @author EX-LILIUZHU001
 * @date 2015年1月28日 下午1:15:07
 * @version V1.0
 */
;
(function(){
	'use strict';	
	var app = angular.module('fwa.mainApp');
	app.controller('IntegrityArtificialScoreCtrl', ['$scope','$stateParams','BaseURL','$modalInstance','Modal','FwaTabsetService','IntegrityMedicalService','CommunityService','WarningScreenService','IntegrityArtificialScoreService','$modal',
	                              function($scope,$stateParams,baseUrl,$modalInstance,Modal,FwaTabsetService,IntegrityMedicalService,CommunityService,WarningScreenService,IntegrityArtificialScoreService,$modal){	  
					  var warningMarks = {
								deducitonMedicals:[],//医疗机构
								deducitonStaffs:[],//医护人员
								deducitonInsurs:[],//参保人员
								deductionDeletes:[]//删除过已存库的数据
								};
//					  var intiParam = function(pointCutTemps){//记分信息
//						  var param = {
//									caseType:'03',// 案件类型
//									caseNo:warningMarkData.caseNo,// 案件编号
//									targetType:'02',// 扣分对象类型(01医疗机构、02医生、03参保人、04药店)
//									targetNo:targetNo,// 扣分对象编号
//									orgCode:warningMarkData.medicalCode// 机构编号
//							};
//						  if(param){
//								WarningScreenService.getPointCutTempScose(param).
//								success(function(res){
//									if(res.pointCutTemps){
//										angular.forEach(res.pointCutTemps),function(datas,index){
//												pointCutTemps.push(datas);
//										   });
//								 		return pointCutTemps;
//									}
//								});
//							}
//						  return pointCutTemps;
//					  }
//					  warningMarks.deducitonStaffs.push(intiParam([]));
//					  warningMarks.deductionDeletes.push(intiParam([]));
					  $scope.form = {};	
						$scope.communityService = CommunityService;
					 	CommunityService.initCodeListDatas();	    
						CommunityService.getBaseCode('{codeType:\'id_type\'}');
					  $scope.warningScreenService = WarningScreenService;
					  $scope.deducitonForm = {};
					//评分类别
					WarningScreenService.getWarningDeduction({scoreType:'1',scoreLevel:'1'}).
					success(function(res){
						$scope.ratingCategorys = res.warningDeductions;
						if(res.warningDeductions){
							$scope.markContentValues = res.warningDeductions[0].pointTotal;//该对象总分值
						}
					});
					 //修改
					  var taskNo=IntegrityMedicalService.taskNo;
					  if(taskNo){
						  IntegrityArtificialScoreService.integrityArtificialScoreInfo({taskNo:taskNo}).success(function(res){
							  if(res.integrityList){
								  $scope.integrityDoctorList=[];
								  $scope.integrityDoctorList=res.integrityList;
							  }
							  if(res.integrityInfo){
								  $scope.form.doctorNo = res.integrityInfo.doctorNo;
								  $scope.form=res.integrityInfo;
								  $scope.form.medicalName=res.integrityInfo.providerCode;
								  $scope.form.medicalNo=res.integrityInfo.providerCode;
								  $scope.form.medicalAddr=res.integrityInfo.fullAddress;
								  $scope.form.date={};
								  $scope.form.date.start=res.integrityInfo.startDate;
								  $scope.form.date.end=res.integrityInfo.endDate;
							  }
							  if(res.pointList){
								  if($scope.ratingCategorys){
									   if(res.pointList[0].ratingCategoryId){
										   $scope.deducitonForm.idStandardscore = res.pointList[0].ratingCategoryId;
										   $scope.deducitonForm.idReason = res.pointList[0].ruleId;
										   $scope.onAssessmentContentClick();
										   $scope.pointReal = res.pointList[0].pointReal;
										   $scope.idPointCut = res.pointList[0].idPointCut;
										}
								  }
								  $scope.form.happenDate=res.pointList[0].happenDate.substr(0,10);								  
								  $scope.form.pauseDate={};
								  $scope.form.pauseDate.end=res.pointList[0].punishDateEnd.substr(0,10);
								  $scope.form.pauseDate.start=res.pointList[0].punishDateStart.substr(0,10);								  
							  }
						  });
					  }
					var clearData = function(){//清除展示值
						$scope.deductionScores = null;
						$scope.deduScores = null;//记分分值
					}
					$scope.$watch('deducitonForm.idStandardscore', function(e,v){//监听评分类别						
						clearData();
						if($scope.isTrue){
							if($scope.form.doctorNo){
								if($scope.deducitonForm.idStandardscore){
									$scope.onAssessmentContentClick();
								}else{
									$scope.assessmentContents = null;
								}
							}else if(!taskNo){
								Modal.alert("证件号为必选项！");
								return null;
								clearData();
							}
						}else{
							$scope.isTrue = true;
						}
					});
					var tableDataDeductionsSum = function(data){//计算分值
						 var pointNum = '0.0';
						 angular.forEach(data,function(datas,index){
								 pointNum = (parseFloat(pointNum) + parseFloat(datas.point)).toFixed(2);
						   });
						return pointNum;
					}
					var remainingMaxVal = function(){//已记分值
						if($scope.idStandardscore){
							var _orgContentPoint = parseFloat('0.0');
							if($scope.orgContentPoint){
								_orgContentPoint = parseFloat($scope.orgContentPoint);
							}
							var pointNum = tableDataDeductionsSum($scope.tableDataDeductions);
							return _orgContentPoint +  parseFloat(pointNum);// + pointTempNum;//可记分值
						}
						return null;
					}
					var maxPointVal = function(pointReal){
						var point = '0.0';//该项可记分值
						if(parseFloat(pointReal) >= parseFloat($scope.markContentValues)){
							point = $scope.maxRuleUpperValues;
						}else{
							point = pointReal;
						}
						return point;
					}
					//校验取分值
					var maxVal = function(pointReal){//当前记分分值
						var point = maxPointVal(pointReal);
						var _scoreVal = remainingMaxVal();//已记分值
						if(_scoreVal){
							_scoreVal = parseFloat($scope.markContentValues) - _scoreVal//剩余可记分值
							if(_scoreVal >= parseFloat(point)){
								return parseFloat(point).toFixed(1);
							}else if(_scoreVal > 0){
								return _scoreVal.toFixed(1);
							}else{
								return '0.0';
							}
						}
						return point;
					}
					var initScores = function(upperValues){//初始化展示记分参数
						$scope.deducitonForm.pointReal = upperValues;
						$scope.deducitonForm.point = maxVal(upperValues.toFixed(1));
						$scope.deduScores = maxVal(upperValues.toFixed(1));
					}
					
					$scope.onAssessmentContentClick = function() {
						clearData();
						angular.forEach($scope.ratingCategorys,function(datas,index){
							   if(datas.idStandardscore === $scope.deducitonForm.idStandardscore){
								   if(datas.idStandardscore){
										WarningScreenService.getWarningDeduction({scoreType:'1',scoreLevel:'2',parentId:datas.idStandardscore}).//scoreLevel:1.评分类型,2.考核内容，3.考核细则
										success(function(res){
											$scope.assessmentContents = res.warningDeductions;
											if($scope.deducitonForm.idStandardscore){
												angular.forEach($scope.ratingCategorys,function(datas,index){
													   if(datas.idStandardscore === $scope.deducitonForm.idStandardscore){
													   		$scope.deducitonForm.ratingCategory = datas.scoreName;
													   		$scope.onAssessmentRulesItemClick({idStandardscore:$scope.deducitonForm.idReason,upperValues:$scope.pointReal});
													   }
												   });
											}
										});
									}
							   }
						   });
					}
					$scope.onAssessmentRulesItemClick = function(data) {//选中考核内容项
						clearData();
						$scope.maxRuleId = data.idStandardscore;
						$scope.maxRuleUpperValues = data.upperValues;
						if(data.idStandardscore){
							WarningScreenService.getWarningDeduction({scoreType:'1',scoreLevel:'3',parentId:data.idStandardscore,idStandardscore:null}).
							success(function(res){
								if($scope.assessmentContents){
									angular.forEach($scope.assessmentContents,function(datas,index){
										   if(datas.idStandardscore === data.idStandardscore){
											   $scope.deducitonForm.id = data.idStandardscore;
											   if(datas.idStandardscore){
												   $scope.deductionScores = '0.0';
												   WarningScreenService.getPointScose({targetNo:$scope.form.doctorNo}).
													success(function(res){
														if(res.pointScose){
															$scope.orgContentPoint = res.pointScose;//考核内容已记分值
															$scope.deductionScores = parseFloat(res.pointScose).toFixed(1);
														}else{
															$scope.orgContentPoint = null;
														}
														//校验记分刷新
														if($scope.idStandardscore == data.idStandardscore){
															initScores(parseFloat(maxVal(data.upperValues)));
														}else{
															$scope.idStandardscore = data.idStandardscore;
															$scope.markUpperValues = data.upperValues;
															initScores(parseFloat(data.upperValues));
														}
													});
											   }
									   		$scope.deducitonForm.assessmentContent = datas.scoreName;
									   		if($scope.deducitonForm.idReason){
												   datas.selected = true;
									   		}
											data.selected = true;//datas.selected == true;//?false:true;
										   }else{
											   datas.selected = false;
										   }
									   });
								}
							});
						}
					}
					  $scope.communityService = CommunityService;
					  CommunityService.initCodeListDatas();	
					  //关闭弹窗
					  $scope.onOk = function() {
						  $modalInstance.dismiss();
					  }
					  $scope.integrityDoctorList=[];
					  //根据证件号码查询医疗机构信息
					  $scope.getIntegrityDoctorInfo= function(){							
							if($scope.form.cardNo&&$scope.form.cardType){
								 $scope.form.doctorNo=null;
								 IntegrityArtificialScoreService.getIntegrityArtificialDoctor({certifiNo:$scope.form.cardNo,certifiType:$scope.form.cardType}).success(function(res){
									  if(res.integrityDoctorInfo&&res.integrityDoctorInfo.length>0){
										  $scope.form.date={};
										  $scope.integrityDoctorList=res.integrityDoctorInfo;
										  $scope.form.doctorName=res.integrityDoctorInfo[0].name;
										  $scope.form.doctorNo=res.integrityDoctorInfo[0].code;
										  $scope.form.date.start=res.integrityDoctorInfo[0].startDate;
										  $scope.form.date.end=res.integrityDoctorInfo[0].endDate;
									  }else{
										  Modal.alert("未查询到数据,请确认证件类型或证件号是否有误！");
									  }
									  
								  });
							}
						}
					  //监听证件类型改变
					  $scope.$watch('form.cardType',function(v){
						  $scope.getIntegrityDoctorInfo();
//						  if(v&&$scope.form.cardNo){
//							  $scope.form.doctorNo=null;
//							  IntegrityArtificialScoreService.getIntegrityArtificialDoctor({certifiNo:$scope.form.cardNo,certifiType:$scope.form.cardType}).success(function(res){
//								  if(res.integrityDoctorInfo&&res.integrityDoctorInfo.length>0){
//									  $scope.integrityDoctorList=[];
//									  $scope.integrityDoctorList=res.integrityDoctorInfo;
//									  $scope.form.doctorName=res.integrityDoctorInfo[0].name;
//									  $scope.form.doctorNo=res.integrityDoctorInfo[0].code;
//								  }	else{
//									  Modal.alert("未查询到数据,请确认证件类型或证件号是否有误！");
//								  }							  
//							  });
//						  }						  
					  });
					//监听执业医院改变
					  $scope.$watch('form.medicalName',function(v){
						  if(v&& $scope.integrityDoctorList){
							  angular.forEach($scope.integrityDoctorList,function(data,index){
									if ($scope.form.medicalName === data.providerCode) {
										$scope.form.medicalNo = data.providerCode;//机构编号
										$scope.form.medicalAddr=data.fullAddress;//机构地址
									}
								});
						  }						  
	  });
					  
					  var caseNo="";
					  var params = function (){//组装参数
							var ratingCategory=null;
							if($scope.deducitonForm.ratingCategory&&!angular.isUndefined($scope.deducitonForm.ratingCategory)){
								ratingCategory = $scope.deducitonForm.ratingCategory;
							}
							var assessmentContent=null;
							if($scope.deducitonForm.assessmentContent&&!angular.isUndefined($scope.deducitonForm.assessmentContent)){
								assessmentContent = $scope.deducitonForm.assessmentContent;
							}
							var assessmentRules=null;
							if($scope.deducitonForm.assessmentRules&&!angular.isUndefined($scope.deducitonForm.assessmentRules)){
								assessmentRules = $scope.deducitonForm.assessmentRules;
							}
							var ratingCategoryId=null;
							if($scope.deducitonForm.idStandardscore&&!angular.isUndefined($scope.deducitonForm.idStandardscore)){
								ratingCategoryId = $scope.deducitonForm.idStandardscore;
							}
							assessmentRules = assessmentContent;
							var pointReal=null;
							if($scope.deducitonForm.pointReal&&!angular.isUndefined($scope.deducitonForm.pointReal)){
								pointReal = parseFloat($scope.deducitonForm.pointReal).toFixed(1);
							}
							var point=null;
							if($scope.deducitonForm.point&&!angular.isUndefined($scope.deducitonForm.point)){
								point = parseFloat($scope.deducitonForm.point).toFixed(1);
							}
							var ruleId = $scope.deducitonForm.id;
							var reason = assessmentRules;
							var happenDate = null,punishDateStart = null,punishDateEnd = null;
//								happenDate = WarningAddService.warningMarkData.punishDateEnd;//案发时间
							if($scope.form.pauseDate){
								punishDateStart=$scope.form.pauseDate.start;
								punishDateEnd=$scope.form.pauseDate.end;
							}
							var idPointCut = null;
							if($scope.idPointCut){
								idPointCut = $scope.idPointCut;
							}
							if(taskNo){
								caseNo=taskNo;
							}
							var params = {
									idPointCut:idPointCut,
									ratingCategory:ratingCategory,
									assessmentContent:assessmentContent,
									assessmentRules:assessmentRules,
									caseType:'03',// 案件类型
									caseNo:caseNo,// 案件编号
									targetType:'02',// 记分对象类型(01医疗机构、02医生、03参保人、04药店)
									targetNo:$scope.form.doctorNo,// 记分对象编号
									orgCode:$scope.form.medicalNo,// 机构编号
									point:point,// 记分值
									pointReal:pointReal,// 实际记分值
									ruleId:ruleId,// 记分规则
									reason:reason,// 记分原因
									happenDate:$scope.form.happenDate,// 案发日期
									punishDateStart:punishDateStart,// 惩罚起期
									punishDateEnd:punishDateEnd,// 惩罚终期
									isFlat:true,//新增记分
									ratingCategoryId:ratingCategoryId
							}
							return params;
						}
					  var verification = function(){//校验扣分
						  if(!$scope.form.doctorName){
								Modal.alert("姓名不能为空!");
								return false;
							}
						  if(!$scope.form.cardType){
								Modal.alert("证件类型不能为空!");
								return false;
							}
						  if(!$scope.form.cardNo){
								Modal.alert("证件号不能为空!");
								return false;
							}
						  if(!$scope.form.medicalName){
								Modal.alert("执业医院不能为空!");
								return false;
							}
						  if(!$scope.form.medicalNo){
								Modal.alert("机构编号不能为空!");
								return false;
							}
						  if(!$scope.form.medicalAddr){
								Modal.alert("机构地址不能为空!");
								return false;
							}
						  /*if(!$scope.form.date.start||!$scope.form.date.end){
								Modal.alert("执业时间不能为空!");
								return false;
							}	*/
						  if(!$scope.form.happenDate){
								Modal.alert("案发时间不能为空!");
								return false;
							}	
						  if(!$scope.form.pauseDate){
								Modal.alert("暂停医保服务权限时间不能为空!");
								return false;
							}
						  if(!$scope.form.pauseDate.start){
								Modal.alert("暂停医保服务权限开始时间不能为空!");
								return false;
							}
						  if(!$scope.form.pauseDate.end){
								Modal.alert("暂停医保服务权限结束时间不能为空!");
								return false;
							}
							var par = params();
							if(!par.ratingCategory){
								Modal.alert("评分类别不能为空！");
								return false;
							}
							if(!par.assessmentContent){
								Modal.alert("考核内容为必选项！");
								return false;
							}							
							if(!/^[\d]*[.]?[\d]?$/.test(par.pointReal)){
								Modal.alert("该初始记分必须为数字或一位小数值！");
								$scope.deducitonForm.pointReal = null;
								return false;
							}
						     return true;
						}
					  //确定事件
					  $scope.confirm = function(saveStatus) {						 
						  if(verification()){
							  if(taskNo){
								  warningMarks.deductionDeletes.push(params());
							  }
							  warningMarks.deducitonStaffs.push(params());						
								var param={
											happenDate:$scope.form.happenDate,// 案发日期
											punishDateStart:$scope.form.pauseDate.start,// 惩罚起期
											punishDateEnd:$scope.form.pauseDate.end,// 惩罚终期
						        		    taskNo:caseNo,
						        		    warnMarkTempDetails:[warningMarks],
						        		    certifiNo:$scope.form.cardNo,
						        		    certifiType:$scope.form.cardType,
						        		    orgCode:$scope.form.medicalNo,
						        		    saveStatus:saveStatus
					                        };	
								$scope.form.loading=true							    
								IntegrityArtificialScoreService.saveIntegrityArtificialDoctor(param).success(function(res){
										Modal.alert(res.msg);								
									  if(res.state=="0"){
										$scope.form.loading=false;
										warningMarks.deducitonStaffs = [];
										$modalInstance.close(true);//执行成功
									  }
						          });							  
						  }						
						//$modalInstance.close($scope.form);
					  }		
}]);
})();
	